<?php

class Cont {
	public $id_cont; 		//id-ul contactului in tabel
	public $id_owner; 		//id-ul firmei pentru care este contactul
	public $tip; 		
    public $cont;            
	public $moneda;			
	public $banca;
	public $sucursala;	
	public $version;	
	public $status;			//0 - Unmodified; 1 - Modified; 2 - New; 3 - Deleted;

	function __construct($id_owner, $tip, $cont, $banca, $sucursala, $moneda) {
		$this->id_cont = CT_NOT_SAVED_YET;
		$this->id_owner = $id_owner;
		$this->tip = $tip;
        $this->cont = $cont;
		$this->moneda = $moneda;
		$this->banca = $banca;
		$this->sucursala = $sucursala;
		$this->version = 0;
		$this->status = CT_CONT_NEW;
	}
	
	public function queryInsertCont($db) {
		$query = $db->prepare("INSERT INTO `cont`  ( 	`id_owner` , 		`tip` , 
														`cont` , 			`banca`,
	                        		        			`sucursala` ,    	`version`,
                                                        `moneda`  
		                               				) 
		            					VALUES  	(	:id_owner,  		:tip, 
		            									:cont,   			:banca,
		            									:sucursala, 		:version,
                                                        :moneda		                      		
		                      						);");
		
		$query->bindParam(':id_owner', 	($this->id_owner));
		$query->bindParam(':tip', 		($this->tip));
		$query->bindParam(':cont', 		($this->cont));
		$query->bindParam(':banca', 	($this->banca));
		$query->bindParam(':sucursala', ($this->sucursala));
        $query->bindParam(':version',     ($this->version));
		$query->bindParam(':moneda', 	($this->moneda));
		
		if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
	  		$query->closeCursor();
			throw new QueryInsertContException("Failed saving cont to the database" . $errInfo);		
		}
	  		
  		$this->id_cont = (int) $db->lastInsertId(); 
  		$query->closeCursor();
  		$this->status = CT_CONT_UNMODIFIED;
		return $this->id_cont; 		//return the cont's id
	}
	
	public function queryUpdate($db) {					       	       
		$this->version++;
		
		$query = $db->prepare("UPDATE `cont`  SET 	`id_owner` = :id_owner , 		`tip` =  :tip, 
									 			  	`cont` = :cont, 				`banca` = :banca,
	                        		 				`sucursala` = :sucursala,    	`version` = :version,
                                                    `moneda` = :moneda 	                        		 	                        		
	                            	WHERE id = :id_cont
	                				AND version < :version");
		
		$query->bindParam(':id_owner', 	($this->id_owner));
		$query->bindParam(':tip', 		($this->tip));
		$query->bindParam(':cont', 		($this->cont));
		$query->bindParam(':banca', 	($this->banca));
		$query->bindParam(':sucursala', ($this->sucursala));
		$query->bindParam(':version', 	($this->version));
        $query->bindParam(':id_cont',     ($this->id_cont));        
		$query->bindParam(':moneda', 	($this->moneda));		
				               
		if ($query->execute() === FALSE) {			
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
			$query->closeCursor();
			throw new QueryUpdateContException("Failed updating cont with id: " . $this->id_cont . $errInfo);
		}
			
		if ($query->rowCount() == 0) {
	       	$query->closeCursor();
			throw new QueryWrongVersionException("Failed updating cont. Reason: wrong version!");
		}
	       
       //reset the status to unmodified for a possible re-edit;
	    $this->status = CT_CONT_UNMODIFIED;	
	    $query->closeCursor();	  	
		return CT_RESULT_OK;
	}
	
  	public static function queryLoadCont($db, $id_cont) {
        $query = $db->prepare("SELECT *
                    			FROM cont
                    			WHERE id = :id_cont");
        $query->bindParam(':id_cont', 	$id_cont);                    			
        
        
        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
        	throw new QueryLoadContException("Faild loading cont with id: " . $id_cont . $errInfo);
        }
        
        $data = $query->fetch(PDO::FETCH_ASSOC);
                   
        if (!isset($data['id'])) {
        	$query->closeCursor();
        	throw new QueryLoadContException("Faild loading cont with id: " . $id_cont . " reason: id not existing");
        }
         
        $cont = new Cont($data['id_owner'], $data['tip'], $data['cont']
                            , $data['banca'], $data['sucursala'], $data['moneda']);
        $cont->id_cont = (int) $data['id'];
        $cont->version = $data['version'];
        $query->closeCursor();
        return $cont;
    }	
    
    function queryDeleteCont($db) {    	
      	$query = $db->prepare("DELETE FROM cont WHERE id = :id_cont");
      	$query->bindParam(':id_cont', 	$this->id_cont);                    			
      	
      	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
      		throw new QueryDeleteContException("Faild deleting cont with id: " . $this->id_cont . $errInfo);
      	}
      	
      	$query->closeCursor();
      	return CT_RESULT_OK;      	
    }
    
    //deletes a contact by id; does not require an object.... STATIC
    public static function deleteContById($db, $id_cont) {
    	$query = $db->prepare("DELETE FROM cont WHERE id = :id_cont");
    	$query->bindParam(':id_cont', 	$id_cont);                    			
    	
    	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
    		throw new QueryDeleteContException("Failed deleting cont with id: " . $this->id_cont . $errInfo);      		
    	}
      	
    	$query->closeCursor();
    	return CT_RESULT_OK;      	
    }
    
    public static function queryLoadContByOwner($db, $tip, $id_owner) {
        $query = $db->prepare("SELECT *
                    FROM cont
                    WHERE id_owner = :id_owner
                        AND tip = :tip");
        
        $query->bindParam(':id_owner', 	$id_owner);                    			
        $query->bindParam(':tip', 		$tip);                    			

        if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
            $query->closeCursor();
        	throw new QueryLoadContException("Faild loading cont for owner with id: " . $id_owner . $errInfo);
        }
                   
	    $conturi = array();
        while ($data = $query->fetch(PDO::FETCH_ASSOC)) {
            $cont = new Cont($data['id_owner'], $data['tip'], $data['cont']
                            , $data['banca'], $data['sucursala'], $data['moneda']);
            $cont->id_cont = (int) $data['id'];
            $cont->version = $data['version'];
            $cont->status = CT_CONT_UNMODIFIED;
            $conturi[] = $cont;
        }        
        $query->closeCursor();
        return $conturi;
    }  
    
    public static function deleteContByIdOwner($db, $id_owner, $tip) {
    	
    	$query = $db->prepare("DELETE FROM cont 
    							WHERE id_owner = :id_owner AND tip = :tip");
    	
    	$query->bindParam(':id_owner', 	$id_owner);                    			
    	$query->bindValue(':tip', 	CT_CONT_FIRMA);                    			
    	
    	if ($query->execute() === FALSE) {
            $errInfo = $query->errorInfo();
            $errInfo = $errInfo[2];
      		$query->closeCursor();
    		throw new QueryDeleteContException("Failed deleting cont with id_owner: " . $id_owner . $errInfo);      		
    	}
      	
    	$query->closeCursor();
    	return CT_RESULT_OK;      	
    }  
}

?>